package cn.ffcs.uec.organization.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.common.manager.CustomFilesManager;
import cn.ffcs.uec.organization.action.bean.UecOrganizationDeptMainBean;
import cn.ffcs.uec.organization.manager.UecOrgDeptManager;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgDept;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.staffAndOutStaff.manager.StaffAndOutStaffManager;
import cn.ffcs.uec.staffAndOutStaff.vo.StaffAndOutStaff;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import lombok.Getter;
import lombok.Setter;

@Controller
@Scope("prototype")
public class UecOrganizationDeptMainComposer extends BasePortletComposer {

    private static final long serialVersionUID = -1231149157686420724L;

    private UecOrganizationDeptMainBean bean = new UecOrganizationDeptMainBean();

    private CustomFilesManager customFilesManager = (CustomFilesManager) ApplicationContextUtil
            .getBean("customFilesManager");

    private UecOrgDeptManager uecOrgDeptManager = (UecOrgDeptManager) ApplicationContextUtil
            .getBean("uecOrgDeptManager");

    private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
            .getBean("uecOrgOrgsManager");

    private StaffAndOutStaffManager staffAndOutStaffManager = (StaffAndOutStaffManager) ApplicationContextUtil
            .getBean("staffAndOutStaffManager");

    private UecOrgDept uecOrgDept = new UecOrgDept();

    /**
     * 操作类型
     */
    private String opType;

    private String operType = null;

    private StaffAndOutStaff staffAndOutStaffLeader;

    private StaffAndOutStaff staffAndOutStaffGlbdef1;

    @Getter
    @Setter
    private IPortletInfoProvider portletInfoProvider;

    @Override
    public String getPortletId() {
        return super.getPortletId();
    }

    @Override
    public ThemeDisplay getThemeDisplay() {
        return super.getThemeDisplay();
    }

    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        Components.wireVariables(comp, bean);
        this.bean.getLeaderNa().addForward("onStaffAndOutStaffEditSelected", comp, "onLeaderEditSelectedResponse");
        this.bean.getGlbdef1Na().addForward("onStaffAndOutStaffEditSelected", comp, "onGlbdef1EditSelectedResponse");
    }

    /**
     * 界面初始化
     */
    public void onCreate$uecOrganizationDeptMainWindow() {
        bindBean();
    }

    /**
     * 监听leaderNa
     */
    public void onFocus$leaderNa() {
        operType = "deptLeaderEdit";
        Events.postEvent("onDeptLeaderEdit", this.bean.getLeaderNa().getStaffAndOutStaffListbox(), operType);
    }

    /**
     * 监听glbdef1Na
     */
    public void onFocus$glbdef1Na() {
        operType = "deptGlbdef1Edit";
        Events.postEvent("onDeptGlbdef1Edit", this.bean.getGlbdef1Na().getStaffAndOutStaffListbox(), operType);
    }

    /**
     * 监听
     */
    public void onLeaderEditSelectedResponse(final ForwardEvent event) {
        if (event.getOrigin().getData() != null) {
            staffAndOutStaffLeader = (StaffAndOutStaff) event.getOrigin().getData();
        }
    }

    /**
     * 监听
     */
    public void onGlbdef1EditSelectedResponse(final ForwardEvent event) {
        if (event.getOrigin().getData() != null) {
            staffAndOutStaffGlbdef1 = (StaffAndOutStaff) event.getOrigin().getData();
        }
    }

    /**
     * 页面赋值
     */
    public void bindBean() {
        opType = (String) arg.get("opType");
        if ("edit".equals(opType)) {
            uecOrgDept = (UecOrgDept) arg.get("uecOrgDept");
            // 如果集团的负责人不为空，则省内的负责人不允许修改
            if (!StrUtil.isNullOrEmpty(uecOrgDept.getPrincipal()) && !"~".equals(uecOrgDept.getPrincipal())) {
                ZkUtil.showError("集团下发的负责人不为空，省内负责人不允许修改！", "温馨提示");
                bean.getLeaderNa().setDisabled(true);
            }
            bean.getUecOrganizationDeptMainWindow().setTitle("部门基本信息修改");
            if (!StrUtil.isNullOrEmpty(uecOrgDept)) {
                PubUtil.fillBeanFromPo(uecOrgDept, bean);
                groupDataTranslationDisplay();
            }
        }
    }

    /**
     * 保存
     * 
     * @throws Exception
     * 
     * @throws Exception
     */
    public void onOk() throws Exception {
        if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
            ZkUtil.showError("当前登录用户不存在!", "错误提示");
            return;
        }
        String currentName = PlatformUtil.getCurrentUser().getScreenName();
        if ("edit".equals(opType)) {
            // 判断是否有值
            if (!StrUtil.isNullOrEmpty(bean.getLeaderNa().getValue())) {
                if ("无".equals(bean.getLeaderNa().getValue())) {
                    uecOrgDept.setLeader("无");
                } else if (!StrUtil.isNullOrEmpty(staffAndOutStaffLeader)) {
                    uecOrgDept.setLeader(staffAndOutStaffLeader.getPkPsndoc());
                }
            } else if (StrUtil.isNullOrEmpty(bean.getLeaderNa().getValue())) {
                uecOrgDept.setLeader(null);
            }
            // 判断是否有值
            if (!StrUtil.isNullOrEmpty(bean.getGlbdef1Na().getValue())) {
                if ("无".equals(bean.getGlbdef1Na().getValue())) {
                    uecOrgDept.setGlbdef1("无");
                } else if (!StrUtil.isNullOrEmpty(staffAndOutStaffGlbdef1)) {
                    uecOrgDept.setGlbdef1(staffAndOutStaffGlbdef1.getPkPsndoc());
                }
            } else if (StrUtil.isNullOrEmpty(bean.getGlbdef1Na().getValue())) {
                uecOrgDept.setGlbdef1(null);
            }
            uecOrgDept.setModifier(currentName);
            uecOrgDept.setModifiedtime(DateUtil.getCurrenDate());
            uecOrgDept.updateOnly();
            Events.postEvent("onOK", this.self, uecOrgDept);
            this.onCancel();
            Messagebox.show("更新成功！");
        }
    }

    /**
     * 取消
     */
    public void onCancel() {
        // 关闭窗口
        this.bean.getUecOrganizationDeptMainWindow().onClose();
    }

    /**
     * 集团字段翻译
     */
    public void groupDataTranslationDisplay() {
        if (null != uecOrgDept) {
            // 负责人--principal
            // bean.getPrincipal()
            // .setValue(staffAndOutStaffManager.getStaffAndOutStaffNameByPkPsndoc(uecOrgDept.getPrincipal()));
            // 负责人（省内）--leader
            // bean.getLeader()
            // .setValue(staffAndOutStaffManager.getStaffAndOutStaffNameByPkPsndoc(uecOrgDept.getLeader()));
            if ("无".equals(uecOrgDept.getLeader())) {
                bean.getLeaderNa().setValue("无");
            } else {
                bean.getLeaderNa()
                        .setValue(staffAndOutStaffManager.getStaffAndOutStaffNameByPkPsndoc(uecOrgDept.getLeader()));
            }
            // 分管领导
            // bean.getGlbdef1()
            // .setValue(staffAndOutStaffManager.getStaffAndOutStaffNameByPkPsndoc(uecOrgDept.getGlbdef1()));
            if ("无".equals(uecOrgDept.getGlbdef1())) {
                bean.getGlbdef1Na().setValue("无");
            } else {
                bean.getGlbdef1Na()
                        .setValue(staffAndOutStaffManager.getStaffAndOutStaffNameByPkPsndoc(uecOrgDept.getGlbdef1()));
            }
            // 上级部门
            if (null != uecOrgDept.getPkFatherorg()) {
                UecOrgDept objUecDept = UecOrgDept.newInstance();
                objUecDept.setPkOrg(uecOrgDept.getPkFatherorg());
                UecOrgDept findUecOrgOrgsByUecOrgDept = uecOrgDeptManager.findUecOrgOrgsByUecOrgDept(objUecDept);
                if (null != findUecOrgOrgsByUecOrgDept) {
                    bean.getPkFatherorg().setValue(findUecOrgOrgsByUecOrgDept.getName());
                }
            }
            // 部门类型
            bean.getGlbdef2().setValue(customFilesManager.findCustomFilesByPkdefdoc(uecOrgDept.getGlbdef2()));
            // 机构类型
            bean.getGlbdef3().setValue(customFilesManager.findCustomFilesByPkdefdoc(uecOrgDept.getGlbdef3()));
            // 渠道类型
            bean.getGlbdef7().setValue(customFilesManager.findCustomFilesByPkdefdoc(uecOrgDept.getGlbdef7()));
            // 服务区域
            bean.getGlbdef11().setValue(customFilesManager.findCustomFilesByPkdefdoc(uecOrgDept.getGlbdef11()));
            // 单位属性
            bean.getGlbdef14().setValue(customFilesManager.findCustomFilesByPkdefdoc(uecOrgDept.getGlbdef14()));
            bean.getGlbdef15().setValue(customFilesManager.findCustomFilesByPkdefdoc(uecOrgDept.getGlbdef15()));
            bean.getGlbdef16().setValue(customFilesManager.findCustomFilesByPkdefdoc(uecOrgDept.getGlbdef16()));
            // 部门级别
            bean.getDeptlevel().setValue(customFilesManager.findCustomFilesByPkdefdoc(uecOrgDept.getDeptlevel()));
            // 所属业务单元
            if (null != uecOrgDept.getPkOrg()) {
                UecOrgOrgs uecOrgOrgs = new UecOrgOrgs();
                uecOrgOrgs.setPkOrg(uecOrgDept.getPkOrg());
                UecOrgOrgs findUecOrgOrgsByUecOrgOrgs = uecOrgOrgsManager.findUecOrgOrgsByUecOrgOrgs(uecOrgOrgs);
                if (null != findUecOrgOrgsByUecOrgOrgs) {
                    bean.getPkOrg().setValue(findUecOrgOrgsByUecOrgOrgs.getName());
                }
            }
            // 是否是承包单元
            if (null != uecOrgDept.getGlbdef10()) {
                String glbdef10 = uecOrgDept.getGlbdef10();
                if ("Y".equals(glbdef10)) {
                    bean.getGlbdef10().setValue("是");
                } else if ("N".equals(glbdef10)) {
                    bean.getGlbdef10().setValue("否");
                }
            }
            // 部门类型
            if (null != uecOrgDept.getDepttype()) {
                Long depttype = uecOrgDept.getDepttype();
                if (0 == depttype) {
                    bean.getDepttype().setValue("内部部门");
                } else if (1 == depttype) {
                    bean.getDepttype().setValue("外部第三方部门");
                }
            }
        }
    }
}
